﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Configuration;

namespace Ec_System.API
{
    public class SendMail
    {
        /// <summary>
        /// 发送者
        /// </summary>
        private string mailFrom { get; set; }

        /// <summary>
        /// 收件人
        /// </summary>
        public string[] mailToArray { get; set; }

        /// <summary>
        /// 抄送
        /// </summary>
        public string[] mailCcArray { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string mailSubject { get; set; }

        /// <summary>
        /// 正文
        /// </summary>
        public string mailBody { get; set; }

        ///// <summary>
        ///// 正文是否是html格式
        ///// </summary>
        //public bool isbodyHtml { get; set; }

        public bool Send()
        {
            string mailServer = ConfigurationManager.AppSettings["mailServer"];
            string mailUser = ConfigurationManager.AppSettings["MailUser"];
            string mailPass = EncryptAndDecrypte.DecrypteString(ConfigurationManager.AppSettings["MailPwd"]);
            string mailFrom = ConfigurationManager.AppSettings["MailFrom"];
            //发送Internet邮件        
            try
            {
                SmtpClient client = new SmtpClient(mailServer);
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(mailUser, mailPass);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage mail = new MailMessage();
                mailFrom = mailFrom ?? mailFrom;
                mail.From = new MailAddress(mailFrom);
                foreach (var item in mailToArray)
                {
                    mail.To.Add(item);
                }
                mail.Subject = mailSubject;
                mail.BodyEncoding = System.Text.Encoding.Default;
                mail.Body = mailBody;
                mail.IsBodyHtml = true;
                client.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    }
}
